﻿var timer=0;
var notifier=0;
function mainOnOpen() {
	//Default values
	options.putDefaultValue("box_date","");
	options.putDefaultValue("box_month","");
	options.putDefaultValue("box_year","");
	options.putDefaultValue("box_hour","00");
	options.putDefaultValue("box_minute","00");
	options.putDefaultValue("box_desc","");
  options.putDefaultValue("box_transp", 100);
  options.putDefaultValue("Opacity", 100);
  options.putDefaultValue("show_timer", false);
  options.putDefaultValue("show_desc", false);

  setOpacity(new_holder, options.getValue("Opacity"));
  plugin.onAddCustomMenuItems = AddCustomMenuItems;

  //Mac
  if (framework.system.machine.manufacturer == "Apple"){
   options.putDefaultValue("show_timer", true);
   options.putDefaultValue("show_desc", true);
  }


  if (options.getValue("show_desc")){
   showDesc();
  }else{
   hideDesc();
  }

  if (options.getValue("show_timer")){
   showTime();
  }else{
   hideTime();
  }

  // Get Saved Values
  day=options.getValue("box_date");
  month=options.getValue("box_month");
  year=options.getValue("box_year");
	hour=options.getValue("box_hour");
	minute=options.getValue("box_minute");
    if (options.getValue("box_desc")==''){
    lbl_event.innerText=LBL_GOTO_OPT;
  }else{
		switch (DATE_FORMAT){
			case 'MM-DD-YYYY':
			lbl_event.innerText=options.getValue("box_desc")+"\n"+month+'/'+day+'/'+year;
			break;
			case 'DD-MM-YYYY':
			lbl_event.innerText=options.getValue("box_desc")+"\n"+day+'/'+month+'/'+year;
			break;
		}
  }
  bkgimg=options.getValue("box_img");
  if (bkgimg==''){
	  bkgimg='stock_images\\background.png';
  }
  box_img.src = bkgimg;
  
  setOpacity(box_img, options.getValue("box_transp"));
  
  if (!timer){
   timer=1;
   doCount();
  }
}

function doCount(){
	THEday = new Date(year,(month-1),day,hour,minute);
	today = new Date();
	if (today.getTime() < THEday.getTime()) {
		rest = THEday.getTime() - today.getTime();
		days=Math.floor(rest / (1000 * 60 * 60 * 24));
		hours=Math.floor((rest % (1000 * 60 * 60 * 24))/(1000 * 60 * 60));
		minutes=Math.floor((rest % (1000 * 60 * 60))/(1000 * 60));
		seconds=Math.floor((rest % (1000 * 60))/(1000));
		//Boxes
		day_txt.innerText=lownumber(days);
		hour_txt.innerText=lownumber(hours);
		minute_txt.innerText=lownumber(minutes);
		second_txt.innerText=lownumber(seconds);
    //Set the caption
    var eventTXT=(options.getValue("box_desc")=='') ? '':LBL_TO + ' ' +options.getValue("box_desc");
    view.caption=REMAINING + ' ' + lownumber(days) + ' ' + LBL_DAY + ' ' + eventTXT + ' - ' + GADGET_NAME;
    notifier=0;
	}else{
		day_txt.innerText="00";
		hour_txt.innerText="00";
		minute_txt.innerText="00";
		second_txt.innerText="00";
    view.caption=GADGET_NAME;
    showNotifier();
    notifier=1;
	}
  //..and wait to do it all again
  setTimeout("doCount()", 1000);
}
function lownumber(number){
	//Check if the number needs a leading zero
	if (number<10){
		return '0'+number;
	}else{
		return number;
	}
}
function hide(obj){
	obj.visible=false;
}
function show(obj){
	obj.visible=true;
}
function hideTime(){
	view.height=118;
  close_time.visible=false;
}
function hideDesc(){
	view.width=120;
}
function showTime(){
	view.height=160;
}
function showDesc(){
	view.width=250;
}
function toggleTime(){
  if (options.getValue("show_timer")){
    hideTime();
    options.putValue("show_timer", false);
  }else{
   showTime();
   options.putValue("show_timer", true);
  }
}
function toggleDesc(){
  if (options.getValue("show_desc")){
    hideDesc();
    options.putValue("show_desc", false);
  }else{
   showDesc();
   options.putValue("show_desc", true);
  }
}

function view_ondock() {
   hideTime();
   options.putValue("show_timer", false);
   hideDesc();
   options.putValue("show_desc", false);
}

function view_onundock() {
   showTime();
   options.putValue("show_timer", true);
   showDesc();
   options.putValue("show_desc", true);
}

//Menu items
function AddCustomMenuItems(menu) {
  //Opacity
  var opacSub = menu.AddPopup(TRANSP_LABEL);
  for (var i=100; i>10; i-=10) {
    var capt = i+"%";
    opacSub.AddItem(capt, (getOpacity()==i)?gddMenuItemFlagChecked:0, onOpacClicked);
  }
  //Time & Desc related
	menu.AddItem(SHOW_DESC, options.getValue("show_desc")?gddMenuItemFlagChecked:0, toggleDesc);
  menu.AddItem(SHOW_TIMER, options.getValue("show_timer")?gddMenuItemFlagChecked:0, toggleTime);
}
//Transparency
function onOpacClicked(itemText) {
    var opac = itemText.substring(0, itemText.indexOf("%"));
    setOpacity(new_holder, opac);
		options.putValue("Opacity", opac);
}
function getOpacity() {
  var opac = new_holder.opacity/(255/100);
  opac = Math.round(opac);
  return opac;
}
//Notifier
function showNotifier(){
  if (notifier==0){
	  var item = new ContentItem();
	  item.heading = GADGET_NAME;
	  item.snippet=(options.getValue("box_desc")=='') ? EVENT_REACHED:options.getValue("box_desc");
	  plugin.AddContentItem(item, gddItemDisplayAsNotification);
    notifier=true;
  }
}